package hk.m4s.lr.repair.test.ui.goods;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.GoodsSignModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.ui.adapter.StyleGridsAdapter;
import hk.m4s.lr.repair.test.utils.MoneyTool;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import hk.m4s.lr.repair.test.views.InnerGridView;
import hk.m4s.lr.repair.test.views.listview.InnerListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/5/11.
 * 规格选择
 */

public class SlectGoodsSignActivity extends Activity {
    private TextView num_text;
    int num = 1;
    private Context context;
    private ImageView shop_logo;
    private TextView shop_select, shop_count, prices;
    private InnerGridView color_gird, size_gird;
    private InnerListView showStyleList;
    private StyleGridsAdapter gridsAdapter;

    private List<GoodsSignModel.PropertyBean> list = new ArrayList<>();
    private List<GoodsSignModel.PriceBean> pricelist = new ArrayList<>();
    private String goCar = "";
    private String colors = "", sizes = "", brand = "";
    private String goods_id;
    private String goodsPrice = "", goodsCount = "", goodsUrl = "",goodsName="",shopId="",detailsId="";
    private String[]style;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_goods_sign);
        context = this;
        getWindow().setLayout(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
        num_text = (TextView) findViewById(R.id.num_text);
        prices = (TextView) findViewById(R.id.prices);
        showStyleList = (InnerListView) findViewById(R.id.showStyleList);
        shop_logo = (ImageView) findViewById(R.id.shop_logo);
        shop_select = (TextView) findViewById(R.id.shop_select);
        goods_id = getIntent().getStringExtra("goods_id");
        goodsCount = getIntent().getStringExtra("goodsCount");
        goodsUrl = getIntent().getStringExtra("goodsUrl");
        goodsName= getIntent().getStringExtra("goodsName");
        shopId= getIntent().getStringExtra("shopId");
        goodsPrice= getIntent().getStringExtra("goodsPrice");
        detailsId= getIntent().getStringExtra("detailsId");
        if (goodsPrice != null) {
            prices.setVisibility(View.VISIBLE);
            prices.setText(MoneyTool.getLocalMoney(goodsPrice));
        }
        goCar = getIntent().getStringExtra("gocar");
        try {
           // Glide.with(context).load(goodsUrl).asBitmap().placeholder(R.mipmap.default2).error(R.mipmap.default2).into(shop_logo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(goodsCount!=null){
            num=Integer.parseInt(goodsCount);
            num_text.setText(String.valueOf(num));
        }
        if(getIntent().getStringExtra("goodsSize")!=null){
            shop_select.setVisibility(View.VISIBLE);
            colors=getIntent().getStringExtra("goodsSize");
            shop_select.setText("已选:" + "“" + colors + "”" + "  ");
        }
        //findGoodsByProperty();
        gridsAdapter = new StyleGridsAdapter(SlectGoodsSignActivity.this, list);
        showStyleList.setAdapter(gridsAdapter);

    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.num_reduce:
                num--;
                if (num < 1) {
                    num = 1;
                    ToastUtil.toast(context, "数量最少为1");
                    num_text.setText(String.valueOf(num));
                    return;
                }
                num_text.setText(String.valueOf(num));
                break;
            case R.id.num_add:
                num++;
                num_text.setText(String.valueOf(num));
                break;
            case R.id.selct_car:
                if(goodsPrice==null||goodsPrice.equals("")){
                    ToastUtil.toast(context,"请选择商品属性");
                    return;
                }
                if (goCar.equals("1")) {
                    //getGoodsInsert();
                } else if(goCar.equals("2")){
//                    Intent intent = new Intent(SlectGoodsSignActivity.this, OrderSureActivity.class);
//                    intent.putExtra("type","1");
//                    GoodsModel model=new GoodsModel();
//                    model.setShop_id(shopId);
//                    model.setGoods_number(num+"");
//                    model.setPrice(goodsPrice);
//                    model.setImages(goodsUrl);
//                    model.setGoods_name(goodsName);
//                    model.setGoods_id(goods_id);
//                    model.setProty(colors);
//                    List<GoodsModel>list=new ArrayList<>();
//                    list.add(model);
//                    intent.putExtra("goods_id", goods_id);
//                    intent.putExtra("goodsCount", num+"");
//                    intent.putExtra("goodsPrice", goodsPrice);
//                    intent.putExtra("goodsUrl", goodsUrl);
//                    intent.putExtra("goodsName",goodsName);
//                    intent.putExtra("colors",colors);
//                    intent.putExtra("shopId",shopId);
//                    intent.putExtra("goodsList",(Serializable) list);
//                    startActivity(intent);
                }else if(goCar.equals("3")){
                    updateShop();
                }
                finish();
                break;
        }
    }


//    public void findGoodsByProperty() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("goods_id", goods_id);
//        AccountSerive.findGoodsByProperty(context, parmas, new ResponseCallback<GoodsSignModel>() {
//            @Override
//            public void onSuccess(GoodsSignModel userinfoModel) {
//                if (userinfoModel.getPrice() != null) {
//                    pricelist.addAll(userinfoModel.getPrice());
//                }
//                if (userinfoModel.getProperty() != null) {
//                    list.addAll(userinfoModel.getProperty());
//                    style=new String[list.size()];
//                    if(!(colors==null||colors.equals(""))) {
//                        gridsAdapter.updateGird(colors);
//                        String[]colorSize=colors.split(",");
//                        for (int j = 0; j < colorSize.length; j++) {
//                                style[j]=colorSize[j];
//                        }
//                    }
//                    gridsAdapter.notifyDataSetChanged();
//
//                }
//
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }

    public void update(int lin,String names) {
        shop_select.setVisibility(View.VISIBLE);
        style[lin]=names;
        if(style==null){
            shop_select.setVisibility(View.GONE);
        }else{
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<style.length;i++){
                if(style[i]!=null) {
                    sb.append(style[i] + ",");
                }
            }
            colors=sb.toString().substring(0,sb.toString().length()-1);
        }

        if (!colors.equals("")) {
            shop_select.setText("已选:" + "“" + colors + "”" + "  ");
        }
        for (int i = 0; i < pricelist.size(); i++) {
            GoodsSignModel.PriceBean bean = pricelist.get(i);
            if (colors.equals(bean.getQueryCriteria())) {
                prices.setVisibility(View.VISIBLE);
                prices.setText(MoneyTool.getLocalMoney(bean.getMoney()));
                goodsPrice=bean.getMoney();
                goodsUrl=bean.getImg();
                try {
                    Glide.with(context).load(bean.getImg()).asBitmap().placeholder(R.mipmap.device_list_no_img).error(R.mipmap.device_list_no_img).into(shop_logo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
//    public void getGoodsInsert() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("goodsId", goods_id);
//        parmas.put("goodsName", goodsName);
//        parmas.put("shopId", shopId);
//        parmas.put("price", goodsPrice);
//        parmas.put("count", num);
//        parmas.put("img", goodsUrl);
//        parmas.put("property", colors);
//        AccountSerive.getGoodsInsert(context, parmas, new ResponseCallback<GoodsModel>() {
//            @Override
//            public void onSuccess(GoodsModel userinfoModel) {
//                EventBus.getDefault().post(new MessageEvent(1));
//                setResult(RESULT_OK, new Intent());
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
//    public void findShopingByUser() {//获取购物车数量
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//
//        AccountSerive.findShopingByUser(context, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel carModel) {
//                shopModelList.clear();
//                if(carModel!=null){
//                    if(carModel!=null) {
//                        if (carModel.getList() != null) {
//                            shopModelList.addAll(carModel.getList());
//                        }
//
//                    }
//                }
//                setResult(RESULT_OK, new Intent());
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//    }
    public void updateShop() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("detailsId", detailsId);
//        parmas.put("price", goodsPrice);
//        parmas.put("count", num);
//        parmas.put("img", goodsUrl);
//        parmas.put("property", colors);
//        AccountSerive.updateColorOrSize(context, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel carModel) {
//                ToastUtil.toast(context,"修改成功");
//
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });

    }
}
